package cn.dujc.superkid.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cn.dujc.core.ui.BaseActivity;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;

/**
 * @author du
 *         date 2018/8/1 下午7:46
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtPhone;
    private EditText mEtPassword;
    private TextView forgetpassword;
    private TextView register;

    @Override
    public int getViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        setTitle("登录");
        mEtPhone = findViewById(R.id.et_phone);
        mEtPassword = findViewById(R.id.et_password);
        forgetpassword = findViewById(R.id.forget_password);
        register = findViewById(R.id.register);
        findViewById(R.id.btn_login).setOnClickListener(this);
        forgetpassword.setOnClickListener(this);
        register.setOnClickListener(this);
        //ActivityStackUtil.getInstance().closeAllExcept(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: {
                final String phone = mEtPhone.getText().toString();
                if (phone.trim().length() == 0) {
                    ToastUtil.showToast(mActivity, "手机号不能为空");
                    return;
                }
                final String password = mEtPassword.getText().toString();
                if (password.length() == 0) {
                    ToastUtil.showToast(mActivity, "密码不能为空");
                    return;
                }
                HttpHelper.getLoginRegister().userLogin(QMap.build().set("username", phone).set("password", password).create())
                        .enqueue(new ResultCallBack<Result<RegisterResult>>(mActivity) {
                            @Override
                            public void onSuccessCallback(int code, Result<RegisterResult> body) {
                                if (body != null) {
                                    ToastUtil.showToast(mActivity, "登录成功");
                                    Preferences.getInstance().saveLoginResult(mActivity, body.getData());
                                    setResult(RESULT_OK);
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        });
            }
            break;
            case R.id.forget_password:
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.register:
                Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent1);
            default:
                break;
        }
    }
}
